package com.zuozuo.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo3 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class c1 = Class.forName("com.zuozuo.reflection.User");

        /*User user = (User) c1.newInstance();
        System.out.println(user);*/

        /*Field[] declaredFields = c1.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }*/
        Constructor constructor = c1.getConstructor(String.class, int.class, double.class);
        User user = (User) constructor.newInstance("zuozuo", 1, 1);
        System.out.println(user);

        Method setName = c1.getMethod("setName", String.class);
        setName.invoke(user, "左左");

        Method method = c1.getMethod("getName", null);
        String name = (String) method.invoke(user, null);
        System.out.println(name);

        Method run = c1.getDeclaredMethod("run", null);
        run.setAccessible(true);
        run.invoke(user, null);


        Field field = c1.getDeclaredField("name");
        field.setAccessible(true);
        field.set(user, "王老五");
        System.out.println(user.getName());

    }
}
